package com.lowca.news.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.lowca.news.constant.Constants.RoleMeta.DEVELOPER;
import com.lowca.news.service.RoleService;
import com.lowca.news.web.jsonbean.TreeNodeBean;

@Controller
public class AdminIndexController {

	@Resource
	private RoleService roleService;

	@RequestMapping(value = "/admin/index.do", method = RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		return "/admin/index";
	}

	@RequestMapping(value = "/admin/get-date.do", method = RequestMethod.GET)
	@ResponseBody
	public String getDate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		long timestamp = System.currentTimeMillis();
		return String.valueOf(timestamp);
	}

	@RequestMapping(value = "/admin/tree-menu.do")
	public @ResponseBody
	List<TreeNodeBean> ajax(HttpServletRequest request) {
		List<TreeNodeBean> nodeList = new ArrayList<TreeNodeBean>();
		TreeNodeBean userNode = new TreeNodeBean("user", false, "用户管理", "用户管理",
				"#");
		TreeNodeBean userListNode = new TreeNodeBean("user.list", true, "用户管理",
				"用户管理&gt;用户列表", "user/list.do");
		// 只有开发者角色，才有权限访问这个路径
		if (roleService.hasAnyRoles(DEVELOPER.getName())) {
			TreeNodeBean userSetRoleNode = new TreeNodeBean("user.setRole",
					true, "权限设置", "用户管理&gt;权限设置", "user/set-role.do");
			userNode.getChildren().add(userSetRoleNode);
		}
		userNode.getChildren().add(userListNode);

		TreeNodeBean articleNode = new TreeNodeBean("article", false, "文章管理",
				"文章管理", "#");
		TreeNodeBean articleListNode = new TreeNodeBean("article.list", true,
				"文章列表", "文章管理&gt;文章列表", "article/list.do");
		articleNode.getChildren().add(articleListNode);
		TreeNodeBean commentNode = new TreeNodeBean(" comment", false, "评论管理",
				"评论管理", "#");
		TreeNodeBean categoryNode = new TreeNodeBean("category", false, "分类管理",
				"分类管理", "#");
		TreeNodeBean categoryAddNode = new TreeNodeBean("category", true,
				"添加分类", "分类管理&gt;添加分类", "category/add.do");
		categoryNode.getChildren().add(categoryAddNode);
		nodeList.add(userNode);
		nodeList.add(articleNode);
		nodeList.add(commentNode);
		nodeList.add(categoryNode);
		return nodeList;
	}
}
